package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import domain.*;

/**
 * Servlet implementation class for Servlet: ListProducts
 *
 */
 public class ListProducts extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ListProducts() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		Collection c = Store.getInstance().getProducts();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Servlet que lista los productos</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h3>Listado de productos</h3>");
		out.println("<table>");
		Iterator it = c.iterator();
		while(it.hasNext()){
			Product p=(Product)it.next();
			out.println("<td>");
			out.println("<tr> "+p.getBrand()+" </tr>"+"<tr> "+p.getModel()+" </tr>"+ "<tr> " + p.getPrice() + "euros "+"</tr>");
			out.println("</td>");
		}
		out.println("</table>");
		out.println("</body>");
		out.println("</html>");
	}   	  	    
}